package com.iss.ljx.menu.controller;

import com.iss.ljx.menu.service.MenuService;
import com.iss.ljx.mybatis.entity.PtMenuEntity;
import com.iss.ljx.mybatis.ztree.ZtreeEntity;
import com.iss.ljx.spring.controller.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/menu")
public class MenuController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private MenuService menuService;

    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/menuPage")
    public String menuPage() {
        return "menu/menuPage";
    }


    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/getMenuTree")
    @ResponseBody
    public List<ZtreeEntity> getMenuTree() {
        return menuService.getMenuTree();
    }

    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/getMenuInfoById")
    @ResponseBody
    public PtMenuEntity getMenuInfoById(@RequestParam("menuId") String menuId) {
        logger.info("menuId:" + menuId);
        return menuService.getMenuInfoById(menuId);
    }

    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/modifyMenuInfoById")
    @ResponseBody
    public String modifyMenuInfoById(PtMenuEntity entity) {
        logger.info(entity);
        menuService.modifyMenuInfoById(entity);
        return getSuccessMsg("menu.modify.success");
    }

    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/addMenuInfoById")
    @ResponseBody
    public String addMenuInfoById(PtMenuEntity entity) {
        menuService.addMenuInfoById(entity);
        return getSuccessMsg("menu.add.success");
    }

    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/deleteMenuInfoById")
    @ResponseBody
    public String deleteMenuInfoById(@RequestParam("menuId") String menuId) {
        logger.info("menuId:" + menuId);
        menuService.deleteMenuInfoById(menuId);
        return getSuccessMsg("menu.delete.success");
    }


    //用户菜单
    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/userMenuPage")
    public String userMenuPage() {
        return "menu/userMenuPage";
    }
    //获取用户菜单
    @Secured("ROLE_gqgy_data_menu")
    @RequestMapping("/getUserMenu")
    @ResponseBody
    public List<PtMenuEntity> getUserMenu(@RequestParam("userUuid")String userId) {
        List<PtMenuEntity> menuEntities=menuService.getUserMenu(userId);
        if (menuEntities!=null){
            return menuEntities;
        }
        return null;
    }
    //添加用户菜单
    @Secured("ROLE_gqgy_data_menu")
    @PostMapping("/setUserMenu")
    @ResponseBody
    public Integer setUserMenu(@RequestParam("userUuid")String userId,@RequestParam("menuId")String menuId) {
        String roleId=menuService.getUserRoleId(userId);
        return menuService.setUserMenu(roleId,menuId);
    }
    //删除用户菜单
    @Secured("ROLE_gqgy_data_menu")
    @PostMapping("/delUserMenu")
    @ResponseBody
    public Integer delUserMenu(@RequestParam("userUuid")String userId,@RequestParam("menuId")String menuId) {
        String roleId=menuService.getUserRoleId(userId);
        return menuService.delUserMenu(roleId,menuId);
    }
}
